package com.yungu.swift.order.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.mapper.IMapper;
import com.yungu.swift.base.service.impl.BaseServiceImpl;
import com.yungu.swift.order.consts.Constants;
import com.yungu.swift.order.dao.OrderComplainMapper;
import com.yungu.swift.order.dao.OrderComplainReplyMapper;
import com.yungu.swift.order.dao.OrderDetailMapper;
import com.yungu.swift.order.model.dto.OrderComplainDto;
import com.yungu.swift.order.model.dto.OrderComplainReplyDto;
import com.yungu.swift.order.model.dto.OrderDetailDto;
import com.yungu.swift.order.model.param.AdminOrderComplainParam;
import com.yungu.swift.order.model.vo.ApiComplainVo;
import com.yungu.swift.order.service.OrderComplainService;
import com.yungu.swift.oss.OSSTemplate;
import com.yungu.swift.oss.annotation.OSSEntrance;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.support.TransactionTemplate;

import java.net.URL;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.yungu.swift.order.config.NacosListener.ALIYUN_CONFIG;


/**
 * OrderComplainServiceImpl
 **/
@Service
public class OrderComplainServiceImpl extends BaseServiceImpl<OrderComplainDto> implements OrderComplainService {
    @Autowired
    private OrderComplainMapper orderComplainMapper;
    @Autowired
    private OrderComplainReplyMapper orderComplainReplyMapper;
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private TransactionTemplate transactionTemplate;
    @OSSEntrance(bucketName = "")
    private OSSTemplate ossTemplate;


    @Override
    protected IMapper<OrderComplainDto> getMapper() {
        return orderComplainMapper;
    }

    @Override
    public ResponseData<Integer> countDriComplain(Map<String, Object> params) {
        return ResponseData.buildSuccessResponse(orderComplainMapper.countDriComplain(params));
    }

    @Override
    public ResponseData<ApiComplainVo> findComplantStatus(String orderUuid, int respondentIdentity) {
        Map<String, Object> params = MapUtils.build();
        params.put("orderUuid", orderUuid);
        params.put("respondentIdentity", respondentIdentity);
        ApiComplainVo apiComplainVo = orderComplainMapper.findComplantStatus(params);
        if (apiComplainVo == null) {
            apiComplainVo = new ApiComplainVo();
            apiComplainVo.setUuid(orderUuid);
            apiComplainVo.setStatus(Constants.COMPLAINT_STATUS_NULL);
        }
        return ResponseData.buildSuccessResponse(apiComplainVo);
    }

    @Override
    public ResponseData<Boolean> handle(AdminOrderComplainParam orderComplainParam) {
        // 投诉
        OrderComplainDto orderComplainDto = new OrderComplainDto();
        orderComplainDto.setUuid(orderComplainParam.getUuid());
        orderComplainDto.setStatus(orderComplainParam.getStatus());
        orderComplainDto.setResult(orderComplainParam.getResult());
        orderComplainDto.setHandlingOn(new Date());
        orderComplainDto.setHandlingBy(orderComplainParam.getUserUuid());
        orderComplainDto.setUpdateOn(new Date());
        orderComplainDto.setUpdateBy(orderComplainParam.getUserUuid());
        // 回复
        OrderComplainReplyDto orderComplainReplyDto = new OrderComplainReplyDto();
        orderComplainReplyDto.setUuid(StringUtils.buildUUID());
        orderComplainReplyDto.setComplainUuid(orderComplainParam.getUuid());
        orderComplainReplyDto.setResult(orderComplainParam.getResult());
        orderComplainReplyDto.setStatus(orderComplainParam.getStatus());
        orderComplainReplyDto.setHandlingOn(new Date());
        orderComplainReplyDto.setHandlingBy(orderComplainParam.getUserUuid());
        orderComplainReplyDto.setCreateOn(new Date());
        orderComplainReplyDto.setCreateBy(orderComplainParam.getUserUuid());

        Boolean execute = transactionTemplate.execute(transactionStatus -> {
            int add = orderComplainReplyMapper.add(orderComplainReplyDto);
            int edit = orderComplainMapper.edit(orderComplainDto);
            return edit > 0 && add > 0;
        });
        return ResponseData.buildSuccessResponse("操作成功", execute);
    }

    @Override
    public ResponseData<String> getTripRecording(String orderUuid) {
        List<OrderDetailDto> list = orderDetailMapper.list(MapUtils.build("orderUuid", orderUuid));
        if (CollectionUtils.isNotEmpty(list) && StringUtils.isNotEmpty(list.get(0).getTripRecording())) {
            ossTemplate.setBucketName(ALIYUN_CONFIG.getTripRecordBucketName());
            Calendar instance = Calendar.getInstance();
            instance.add(Calendar.MINUTE, 10);
            URL url = ossTemplate.generatePresignedUrl(list.get(0).getTripRecording(), instance.getTime());
            return ResponseData.buildSuccessResponse(url.toString());
        }
        return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "无行程录音信息");
    }

    @Override
    public ResponseData<List<OrderComplainDto>> getList(Map map) {
        return ResponseData.buildSuccessResponse(orderComplainMapper.list(map));
    }

    @Override
    public ResponseData<OrderComplainDto> get(String uuid) {
        OrderComplainDto orderComplainDto = this.orderComplainMapper.getDetail(uuid);
        if (orderComplainDto != null && orderComplainDto.getStatus() != OrderComplainDto.STATUS_PENDING) {
            //投诉处理回复记录
            orderComplainDto.setOrderComplainReplyDtoList(orderComplainReplyMapper.list(MapUtils.build("complainUuid",
                    orderComplainDto.getUuid())));
        }
        return ResponseData.buildSuccessResponse(orderComplainDto);
    }
}
